package routers

import (
	"github.com/gin-gonic/gin"
	"ruoyi/config"
	"ruoyi/infrastructure/persistence"
	"ruoyi/interfaces/middleware"
	"ruoyi/routers/all"
)

// Init 初始化
func Init(s *persistence.SystemRepositories, m *persistence.MonitorRepositories) *gin.Engine {
	gin.SetMode(config.Server.RunMode)
	r := gin.New()
	r.Use(middleware.Recover)
	r.Use(Options)
	all.InitTest(r, s, m)
	// 设置信任网络 []string
	// nil 为不计算，避免性能消耗，上线应当设置
	_ = r.SetTrustedProxies(nil)
	return r
}

func Options(c *gin.Context) {
	print("当前请求路径：{}")
	print(c.Request.URL.String())
	if c.Request.Method != "OPTIONS" {
		c.Next()
	} else {
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,OPTIONS")
		c.Header("Access-Control-Allow-Headers", "authorization, origin, content-type, accept")
		c.Header("Allow", "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS")
		c.Header("Content-Type", "application/json")
		c.AbortWithStatus(200)
	}
}
